package com.bolt.support.query.toolkit;

import com.bolt.common.collection.CollectionUtil;
import com.bolt.common.utils.ArrayUtil;
import com.bolt.common.utils.NumberUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.convention.exception.ServiceException;
import com.bolt.support.query.calculator.domain.UnpivotModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UnpivotResult {



    public static int[] findNotTransformColumnIndex(int rowSize, int[] unpivotCols) {
        int[] rowIndex = java.util.stream.IntStream.range(0, rowSize).toArray();
        return Arrays.stream(rowIndex).filter(k -> ArrayUtil.indexOf(unpivotCols, k) == -1).toArray();
    }

    public static List process(UnpivotModel unpivotModel, List<?> result, List<String> aliases) {
        if (result == null || result.isEmpty() || !result.get(0).getClass().isArray()) {
            return result;
        }
        try {
            int cols = unpivotModel.getTransformColumns().size();
            List newResult = new ArrayList();
            int[] unpivotColsIndex = new int[cols];
            String[] unpivotColValues = new String[cols];
            int[] notUnpivotColsIndex = null;

            String[] colsSet = ArrayUtil.toArray(unpivotModel.getTransformColumns().keySet(), String.class);
            String colValue;
            for (int i = 0; i < cols; i++) {
                colValue = StrUtil.trim(colsSet[i]);
                if (NumberUtil.isInteger(colValue)) {
                    unpivotColsIndex[i] = Integer.parseInt(colValue);
                } else {
                    unpivotColsIndex[i] = JPAUtil.findColumnIndex(aliases, colValue);
                }
                unpivotColValues[i] = unpivotModel.getTransformColumns().get(colsSet[i]);
            }

            notUnpivotColsIndex = findNotTransformColumnIndex(aliases.size(), unpivotColsIndex);

            // 将多列转成行，记录数量是列的倍数
            int size = result.size() * cols;
            for (int i = 0; i < size; i++) {

                Object[] row = (Object[]) result.get(i / cols);
                Object[] newRow = new Object[row.length - cols + 2];
                for (int k = 0; k < notUnpivotColsIndex.length; k++) {
                    newRow[k] = row[notUnpivotColsIndex[k]];
                }
                String typeValue = unpivotColValues[i % cols];
                Object newValue = row[unpivotColsIndex[i % cols]];
                newRow[newRow.length - 2] = typeValue;
                newRow[newRow.length - 1] = newValue;
                newResult.add(newRow);
            }

            List<String> removeItem = new ArrayList<>();
            for (int k : unpivotColsIndex) {
                removeItem.add(aliases.get(k));
            }
            aliases.removeAll(removeItem);
            aliases.addAll(CollectionUtil.arrayToList(unpivotModel.getColumnsLabels()));
            return newResult;

        } catch (Exception ex) {
            throw new ServiceException(ex);
        }
    }


}